package com.leyou.item.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.exception.pojo.ExceptionEnum;
import com.leyou.common.exception.pojo.LyException;
import com.leyou.common.pojo.PageResult;
import com.leyou.item.mapper.BrandMapper;
import com.ly.plus.entity.Brand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class BrandService extends ServiceImpl<BrandMapper, Brand> {
    @Autowired(required = false)
    private BrandMapper brandMapper;

    /**
     * 分页查询品牌信息
     * @param key
     * @param page
     * @param rows
     * @param sortBy
     * @param desc
     * @return
     */
    public PageResult<Brand> brandPageQuery(String key, Integer page, Integer rows, String sortBy, Boolean desc) {
        //分页查询条件
        IPage<Brand> ipage = new Page<>(page,rows);
        //获取条件对象
        QueryWrapper<Brand> wrapper = Wrappers.query();
        //封装条件
        if (StringUtils.isNotEmpty(key)) {
            //搜索条件封装
            wrapper.eq("id",key).or().like("name",key).or().eq("letter",key);
        }
        //排序字段封装
        if (StringUtils.isNotEmpty(sortBy)){
            if (desc){
                //正排序
                wrapper.orderByDesc(sortBy);
            }else {
                //倒序
                wrapper.orderByAsc(sortBy);
            }

        }
        //分页查询
        ipage=page(ipage, wrapper);
        //封装返回数据
        PageResult<Brand> pageResult = new PageResult<>(
                ipage.getTotal(),
                ipage.getPages(),
                ipage.getRecords()
        );
        return pageResult;

    }

    /**
     * 添加品牌信息
     * @param brand
     * @param cids
     */
    public void addBrand(Brand brand, List<Long> cids) {

        try {
            //添加品牌
            super.save(brand);
            //维护中间表
            brandMapper.saveCategoryAndBrand(cids,brand.getId());
        } catch (Exception e) {
            e.printStackTrace();
            //返回文件添加失败信息
            throw  new LyException(ExceptionEnum.FILE_WRITER_ERROR);
        }

    }

    /**
     * 根据id查询品牌
     * @param id
     * @return
     */
    public Brand findBrandById(Long id) {
        Brand brand = brandMapper.selectById(id);
        if (brand==null){
            throw  new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return brand;
    }
    /**
     * 根据分类id查询品牌(多对多)
     * @param id
     * @return
     */
    public List<Brand> fingBrandByCid(Long id) {
        List<Brand>brands=brandMapper.fingBrandByCid(id);
        if (CollectionUtils.isEmpty(brands)){
            throw  new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return brands;
    }

    /**
     * 根据id集合获取品牌集合
     * @return
     */
    public List<Brand> findBrandByIds(List<Long> ids) {
        List<Brand> brands = brandMapper.selectBatchIds(ids);
        if (CollectionUtils.isEmpty(brands)){
            throw  new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return brands;
    }
}
